var express = require('express');
var mysql = require('mysql');
var app = express();
app.set('view engine', 'html');
app.engine('html', require('hbs').__express);

app.use(express.static('public'))

app.get('/demo', function (req, res) {

    var data = { "name": "Alan", "hometown": "Somewhere, TX",
             "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
    res.render('index', data);
});
app.get('/', function (req, res) {
  

    var connection = mysql.createConnection({
        host: '127.0.0.1',
        user: 'root',
        password: 'root',
        database: 'adminstore'
    });

    connection.connect();
    connection.query('SELECT * from menu', function (error, results, fields) {
        if (error) throw error;
        for (const key in fields) {
            //console.info(key);
        }
        var str="";
        for (let index = 0; index < results.length; index++) {
            const element = results[index];
            str=str+element.name+"<br/>";
        }
        res.send(str);
    });

})

var server = app.listen(8081, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

})